package com.sims.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.sims.eo.SimsClass;
import com.sims.service.IClassService;
import com.sims.service.impl.ClassServiceImp;

public class ClassPageAction extends ActionSupport {
	private String classId;
	private SimsClass simsClass = null;
	IClassService classService = new ClassServiceImp();

	// -----------分页获取数据---------------
	private int pageNo = 1;
	private int pageSize = 4;
	private int pageCount;
	private long rowCount;

	public String getClassId() {
		return classId;
	}

	public void setClassId(String classId) {
		this.classId = classId;
	}

	public SimsClass getSimsClass() {
		return simsClass;
	}

	public void setSimsClass(SimsClass simsClass) {
		this.simsClass = simsClass;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public long getRowCount() {
		return rowCount;
	}

	public void setRowCount(long rowCount) {
		this.rowCount = rowCount;
	}

	// 获取所有账号
	public String listClass() throws Exception {
		System.out.println("-----listClassPage");
		rowCount = classService.getRowCount();
		// 获取总页数
		if (rowCount % pageSize == 0) {
			pageCount = (int) (rowCount / pageSize);
		} else {
			pageCount = (int) (rowCount / pageSize) + 1;
		}
		if (pageNo < 1) {
			pageNo = 1;
		} else if (pageNo > pageCount) {
			pageNo = pageCount;
		}
		// 分页获取记录
		List<SimsClass> list = classService.getAllSimsClass(pageNo, pageSize);
		ActionContext ctx = ActionContext.getContext();
		ctx.put("ClassList", list);
		return "list";
	}

	// 删除账号
	public String deleteClass() throws Exception {
		System.out.println("-----deleteClass id=" + classId);
		Long id = Long.parseLong(classId);
		boolean bFlag = classService.deletSimsClassById(id);
		if (bFlag) {
			this.addActionMessage("删除数据成功！");
		} else {
			this.addActionMessage("删除数据失败！");
		}
		return listClass();
	}

	// 修改账号
	public String editClass() throws Exception {
		System.out.println("-----editClass id=" + classId);
		Long id = Long.parseLong(classId);
		simsClass = classService.getSimsClassById(id);
		if (simsClass == null) {
			// 未获取到用户对象给提示
			this.addActionError("id=" + id + ",未获取到用户对象！");
		}
		return "edit";
	}

	// 编辑账号
	@InputConfig(resultName = "edit_input")
	public String updateClass() throws Exception {
		System.out.println("-----updateClass");
		boolean bFlag = classService.updateSimsClass(simsClass);
		if (bFlag) {
			// 修改成功
			return "go_list";
		} else {
			this.addActionError(simsClass.getClassName() + ",您填写的班级名称已存在！请重新填写！");
			return "edit";
		}
	}

	// 编辑账号Action校验方法
	public void validateUpdateClass() throws Exception {
		System.out.println("----validateUpdateClass");
		if (simsClass == null) {
			// 未获取到班级对象给提示
			this.addActionError("未获取到班级对象！");
		} else {
			if (simsClass.getClassCode() == null
					|| simsClass.getClassCode().trim().length() <= 0) {
				this.addFieldError("classCodeInfo", "请输入班级代码 ！");
			}
			if (simsClass.getClassName() == null
					|| simsClass.getClassName().trim().length() <= 0) {
				this.addFieldError("classNameInfo", "请输入班级名称！");
			}
			if (simsClass.getDepartment() == null
					|| simsClass.getDepartment().trim().length() <= 0) {
				this.addFieldError("departmentInfo", "请填写系部！");
			}
			if (simsClass.getMajor() == null
					|| simsClass.getMajor().trim().length() <= 0) {
				this.addFieldError("majorInfo", "请填写专业 ！");
			}
		}
	}

	@InputConfig(resultName = "create_input")
	public String createClass() throws Exception {
		System.out.println("----createClass---");
		IClassService classService = new ClassServiceImp();
		Long id2 = classService.createClass(simsClass);
		if (id2 > 0) {
			// 新建成功
			this.addActionError(simsClass.getClassName() + ",新建成功！");
			return listClass();
		} else {
			// 新建失败
			this.addActionError(simsClass.getClassName() + ",您填写的班级名称已存在！请重新填写！");
			return "create";
		}
	}

	// 创建Action校验方法
	public void validateCreateClass() throws Exception {
		System.out.println("----validateCreateClass---");
		if (simsClass == null) {
			// 未获取到班级对象给提示
			this.addActionError("未获取到班级对象！");
		} else {
			if (simsClass.getClassCode() == null
					|| simsClass.getClassCode().trim().length() <= 0) {
				this.addFieldError("classCodeInfo", "请输入班级代码 ！");
			}
			if (simsClass.getClassName() == null
					|| simsClass.getClassName().trim().length() <= 0) {
				this.addFieldError("classNameInfo", "请输入班级名称！");
			}
			if (simsClass.getDepartment() == null
					|| simsClass.getDepartment().trim().length() <= 0) {
				this.addFieldError("departmentInfo", "请填写系部！");
			}
			if (simsClass.getMajor() == null
					|| simsClass.getMajor().trim().length() <= 0) {
				this.addFieldError("majorInfo", "请填写专业 ！");
			}
		}
	}
}
